import {getRequest} from "./api";


export const initMenu = (router, store) => {
    if (store.state.routes.length > 0) {
        return;
    }
    getRequest('/sys/menu').then(res =>{
        if (res) {
            let fmtRouters = formatRouters(res.obj);
            // 把分装好的数据，存到store中
            router.addRoutes(fmtRouters);
            // 调用 store/index.js 中initRoutes 方法
            // 第一个参数方法名
            store.commit('initRoutes', fmtRouters);
        }
    })
};
export const formatRouters = (routers) => {
    let fmRouters = [];
    routers.forEach(router => {
        let {
            path,
            component,
            name,
            iconCls,
            meta,
            children
        } = router;
        if (children && children instanceof Array) {
            children = formatRouters(children);
        }
        let fmRouter = {
            path:path,
            name:name,
            iconCls:iconCls,
            meta:meta,
            children:children,
            component(resolve) {
                console.log("comp="+component);
                // 未来我会把不同的页面存在不同的文件夹中，
                if (component.startsWith("Home")){
                    require(['../views/'+component+'.vue'],resolve);
                }else if (component.startsWith("Emp")){
                    require(['../views/emp/'+component+'.vue'],resolve);
                } else if (component.startsWith("Per")){
                    require(['../views/per/'+component+'.vue'],resolve);
                } else if (component.startsWith("Sal")){
                    require(['../views/sal/'+component+'.vue'],resolve);
                } else if (component.startsWith("Sta")){
                    require(['../views/sta/'+component+'.vue'],resolve);
                } else if (component.startsWith("Sys")){
                    require(['../views/sys/'+component+'.vue'],resolve);
                }

            }
        }
        fmRouters.push(fmRouter)
    })
    return fmRouters;
}
